package com.example.chatplatform.service.impl;

import com.example.chatplatform.model.dto.AlterPasswordDTO;
import com.example.chatplatform.model.entity.User;
import com.example.chatplatform.mapper.UserMapper;
import com.example.chatplatform.model.vo.*;
import com.example.chatplatform.result.Result;
import com.example.chatplatform.service.UserService;
import com.example.chatplatform.ws.ChatEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public ChatEndpoint chatEndpoint;

    @Override
    public Integer user(User user) {
        return userMapper.user(user);
    }

    @Override
    public Integer userRegister(User user, String FileUrl) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        user.setUserTime(formattedDateTime);
        return userMapper.userRegister(user, FileUrl);
    }

    @Override
    public User userLogin(String userAccount, String userPassword) {
        User user = userMapper.userLogin(userAccount, userPassword);
        if(user == null){
            return null;
        }else {
            return user;
        }
    }

    @Override
    public User userInfo(Integer userId) {
        return userMapper.userInfo(userId);
    }

    @Override
    public boolean alterUser(User user) {
        return userMapper.alterUser(user) > 0;
    }

    @Override
    public int alterPassword(AlterPasswordDTO alterPasswordDTO) {
        String password = userMapper.password(alterPasswordDTO.getUserId());
        if (Objects.equals(password, alterPasswordDTO.getOldPassword())) {
            userMapper.alterPassword(alterPasswordDTO.getUserId(), alterPasswordDTO.getNewPassword());
            return 1;
        } else if (!Objects.equals(password, alterPasswordDTO.getOldPassword())) {
            return 2;
        } else {
            return 3;
        }

    }

    @Override
    public String avatar(Integer userId) {
        return userMapper.avatar(userId);
    }

    @Override
    public boolean alterUserWithAvatar(User user, String fileUrl) {
        return userMapper.alterUserWithAvatar(user, fileUrl) > 0;
    }

    @Override
    public boolean deleteUser(Integer userId, String deleteUserAvatar) {
        userMapper.deleteUserCrony(userId);
        userMapper.deleteUserCronyMessage(userId);
        List<Integer> userGroup = userMapper.userGroup(userId);
        if (userGroup != null) {
            for (Integer item : userGroup) {
                userMapper.deleteGroupUser(userId, item);
                Integer i = userMapper.groupNumber(item);
                userMapper.updateGroupNumber(item, i);
            }
        }
        return userMapper.deleteUser(userId, "该用户已注销", deleteUserAvatar) > 0 &&
                userMapper.addDeleteUser(userId) > 0;
    }

    @Override
    public List getObjectList(Integer userId, String currentType) {
        if (Objects.equals(currentType, "crony")) {
            List<CronyInfoVO> cronyInfoVOS =  userMapper.cronyInfo(userId);
            for (CronyInfoVO item : cronyInfoVOS) {
                if (chatEndpoint.onlineUser(item.getUserId()))  {
                    item.setUserStatus("在线");
                } else {
                    item.setUserStatus("离线");
                }
            }
            return cronyInfoVOS;
        } else if (Objects.equals(currentType, "group")) {
            List<GroupInfoVO> groupInfoVOS =  userMapper.groupInfo(userId);
            return groupInfoVOS;
        } else {
            return null;
        }
    }

    @Override
    public List searchObject(String currentType, String searchObjectValue) {
        if (Objects.equals(currentType, "crony")) {
            List<SearchUserVO> cronyInfoVOS =  userMapper.searchCronyInfo(searchObjectValue);
            List<Integer> deleteUserId = userMapper.deleteUserId();
            if (deleteUserId != null) {
                cronyInfoVOS.removeIf(item -> deleteUserId.contains(item.getUserId()));
            }
            return cronyInfoVOS;
        } else if (Objects.equals(currentType, "group")) {
            List<SearchGroupVO> groupInfoVOS =  userMapper.searchGroupInfo(searchObjectValue);
            List<Integer> deleteGroupId = userMapper.deleteGroupId();
            if (deleteGroupId != null) {
                groupInfoVOS.removeIf(item -> deleteGroupId.contains(item.getGroupId()));
            }
            return groupInfoVOS;
        } else {
            return null;
        }
    }

    @Override
    public UserDetailVO getUserInfo(Integer userId, Integer cronyId) {
        UserDetailVO userDetailVO = userMapper.getUserInfo(cronyId);
        if (userMapper.ifUserCrony(userId, cronyId) == null) {
            userDetailVO.setIfUserCrony(0);
        } else {
            userDetailVO.setIfUserCrony(1);
        }
        if (chatEndpoint.onlineUser(userDetailVO.getUserId()))  {
            userDetailVO.setUserStatus("在线");
        } else {
            userDetailVO.setUserStatus("离线");
        }
        return userDetailVO;
    }

}
